<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>智能写作助手</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        :root {
            --primary: #8B5CF6;
            --secondary: #6D28D9;
        }
        body {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            font-family: 'Segoe UI', system-ui, sans-serif;
        }
        .app-container {
            max-width: 1000px;
            margin: 20px auto;
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        .header {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            color: white;
            padding: 30px;
            text-align: center;
        }
        .feature-card {
            border: none;
            border-radius: 15px;
            transition: all 0.3s ease;
            cursor: pointer;
        }
        .feature-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .feature-card.active {
            border: 2px solid var(--primary);
        }
        .input-area {
            border-radius: 15px;
            border: 2px solid #e2e8f0;
            transition: all 0.3s ease;
        }
        .input-area:focus {
            border-color: var(--primary);
            box-shadow: 0 0 0 3px rgba(139, 92, 246, 0.1);
        }
        .result-area {
            background: #f8fafc;
            border-radius: 15px;
            min-height: 200px;
            white-space: pre-wrap;
            line-height: 1.6;
        }
        .btn-primary {
            background: linear-gradient(135deg, var(--primary), var(--secondary));
            border: none;
            border-radius: 10px;
            padding: 12px 30px;
            font-weight: 600;
        }
        .loading {
            display: none;
            text-align: center;
            padding: 20px;
        }
        .typing-dots {
            display: inline-block;
        }
        .typing-dots span {
            animation: typing 1.4s infinite;
            display: inline-block;
        }
        .typing-dots span:nth-child(2) { animation-delay: 0.2s; }
        .typing-dots span:nth-child(3) { animation-delay: 0.4s; }
        @keyframes typing {
            0%, 60%, 100% { transform: translateY(0); }
            30% { transform: translateY(-10px); }
        }
    </style>
</head>
<body>
    <div class="app-container">
        <!-- 头部 -->
        <div class="header">
            <h1><i class="fas fa-pen-fancy me-2"></i>智能写作助手</h1>
            <p class="mb-0">AI助力，让写作更轻松</p>
        </div>

        <!-- 主体内容 -->
        <div class="container-fluid p-4">
            <div class="row">
                <!-- 功能选择 -->
                <div class="col-md-4">
                    <div class="row g-3">
                        <div class="col-12">
                            <div class="feature-card card p-3 active" data-action="generate_article">
                                <div class="card-body text-center">
                                    <i class="fas fa-file-alt fa-2x text-primary mb-2"></i>
                                    <h5>文章生成</h5>
                                    <small class="text-muted">根据主题生成完整文章</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="feature-card card p-3" data-action="improve_writing">
                                <div class="card-body text-center">
                                    <i class="fas fa-magic fa-2x text-success mb-2"></i>
                                    <h5>文本优化</h5>
                                    <small class="text-muted">润色和改进现有文本</small>
                                </div>
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="feature-card card p-3" data-action="generate_outline">
                                <div class="card-body text-center">
                                    <i class="fas fa-sitemap fa-2x text-warning mb-2"></i>
                                    <h5>大纲生成</h5>
                                    <small class="text-muted">创建文章结构大纲</small>
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 选项设置 -->
                    <div class="mt-4" id="options-panel">
                        <div class="option-group mb-3">
                            <label class="form-label">文章风格</label>
                            <select class="form-select" id="style-select">
                                <option value="正式">正式</option>
                                <option value="轻松">轻松</option>
                                <option value="学术">学术</option>
                                <option value="创意">创意</option>
                            </select>
                        </div>
                        <div class="option-group mb-3">
                            <label class="form-label">文章长度</label>
                            <select class="form-select" id="length-select">
                                <option value="短篇">短篇 (300字)</option>
                                <option value="中等" selected>中等 (500字)</option>
                                <option value="长篇">长篇 (800字)</option>
                            </select>
                        </div>
                        <div class="option-group mb-3" id="improvement-options" style="display: none;">
                            <label class="form-label">优化类型</label>
                            <select class="form-select" id="improvement-select">
                                <option value="语法优化">语法优化</option>
                                <option value="润色提升">润色提升</option>
                                <option value="简洁表达">简洁表达</option>
                                <option value="正式化">正式化</option>
                            </select>
                        </div>
                    </div>
                </div>

                <!-- 输入输出区域 -->
                <div class="col-md-8">
                    <div class="input-group mb-3">
                        <span class="input-group-text"><i class="fas fa-edit"></i></span>
                        <input type="text" class="form-control input-area" id="topic-input" 
                               placeholder="请输入文章主题或问题...">
                    </div>
                    
                    <div class="mb-3" id="text-input-group" style="display: none;">
                        <label class="form-label">待优化文本</label>
                        <textarea class="form-control input-area" id="text-input" 
                                  rows="4" placeholder="请输入需要优化的文本..."></textarea>
                    </div>
                    
                    <button class="btn btn-primary w-100 mb-3" id="generate-btn">
                        <i class="fas fa-bolt me-2"></i>开始生成
                    </button>

                    <!-- 加载指示器 -->
                    <div class="loading" id="loading-indicator">
                        <div class="typing-dots">
                            <span>.</span><span>.</span><span>.</span>
                        </div>
                        <div>AI正在思考中，请稍候...</div>
                    </div>

                    <!-- 结果展示 -->
                    <div class="result-area p-4" id="result-area">
                        <div class="text-center text-muted">
                            <p>生成结果将显示在这里</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        class WritingAssistant {
            constructor() {
                this.currentAction = 'generate_article';
                this.isGenerating = false;
                this.init();
            }

            init() {
                this.bindEvents();
                this.updateUI();
            }

            bindEvents() {
                // 功能卡片点击事件
                document.querySelectorAll('.feature-card').forEach(card => {
                    card.addEventListener('click', (e) => {
                        this.selectAction(e.currentTarget.dataset.action);
                    });
                });

                // 生成按钮点击事件
                document.getElementById('generate-btn').addEventListener('click', () => {
                    this.generateContent();
                });

                // 输入框回车事件
                document.getElementById('topic-input').addEventListener('keypress', (e) => {
                    if (e.key === 'Enter' && !this.isGenerating) {
                        this.generateContent();
                    }
                });
            }

            selectAction(action) {
                this.currentAction = action;
                
                // 更新卡片状态
                document.querySelectorAll('.feature-card').forEach(card => {
                    card.classList.toggle('active', card.dataset.action === action);
                });

                this.updateUI();
            }

            updateUI() {
                const topicInput = document.getElementById('topic-input');
                const textInputGroup = document.getElementById('text-input-group');
                const improvementOptions = document.getElementById('improvement-options');

                // 根据功能类型更新UI
                if (this.currentAction === 'improve_writing') {
                    topicInput.placeholder = '请输入需要优化的文本主题...';
                    textInputGroup.style.display = 'block';
                    improvementOptions.style.display = 'block';
                } else if (this.currentAction === 'generate_outline') {
                    topicInput.placeholder = '请输入文章主题来生成大纲...';
                    textInputGroup.style.display = 'none';
                    improvementOptions.style.display = 'none';
                } else {
                    topicInput.placeholder = '请输入文章主题...';
                    textInputGroup.style.display = 'none';
                    improvementOptions.style.display = 'none';
                }
            }

            async generateContent() {
                if (this.isGenerating) return;

                const topic = document.getElementById('topic-input').value.trim();
                const text = document.getElementById('text-input').value.trim();

                if (!topic && !text) {
                    alert('请输入内容！');
                    return;
                }

                this.isGenerating = true;
                this.showLoading(true);

                try {
                    const response = await fetch('/api/generate', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json',
                        },
                        body: JSON.stringify({
                            action: this.currentAction,
                            topic: topic,
                            text: text,
                            style: document.getElementById('style-select').value,
                            length: document.getElementById('length-select').value,
                            improvement_type: document.getElementById('improvement-select').value
                        })
                    });

                    const data = await response.json();

                    if (data.success) {
                        this.showResult(data.result);
                    } else {
                        this.showError(data.error || '生成失败');
                    }
                } catch (error) {
                    console.error('请求失败:', error);
                    this.showError('网络错误，请稍后重试');
                } finally {
                    this.isGenerating = false;
                    this.showLoading(false);
                }
            }

            showLoading(show) {
                const btn = document.getElementById('generate-btn');
                const loading = document.getElementById('loading-indicator');
                const result = document.getElementById('result-area');

                if (show) {
                    btn.disabled = true;
                    btn.innerHTML = '<i class="fas fa-spinner fa-spin me-2"></i>生成中...';
                    loading.style.display = 'block';
                    result.style.display = 'none';
                } else {
                    btn.disabled = false;
                    btn.innerHTML = '<i class="fas fa-bolt me-2"></i>开始生成';
                    loading.style.display = 'none';
                    result.style.display = 'block';
                }
            }

            showResult(content) {
                const resultArea = document.getElementById('result-area');
                resultArea.innerHTML = `
                    <div class="result-content">
                        <div class="content-text">${this.formatContent(content)}</div>
                    </div>
                `;
            }

            showError(error) {
                const resultArea = document.getElementById('result-area');
                resultArea.innerHTML = `
                    <div class="text-center text-danger">
                        <i class="fas fa-exclamation-triangle fa-2x mb-2"></i>
                        <p>${error}</p>
                        <button class="btn btn-outline-secondary btn-sm" onclick="writingAssistant.retry()">
                            重试
                        </button>
                    </div>
                `;
            }

            formatContent(content) {
                // 简单的格式处理
                return content.replace(/\n/g, '<br>');
            }

            async copyToClipboard() {
                const content = document.querySelector('.content-text').innerText;
                try {
                    await navigator.clipboard.writeText(content);
                    alert('内容已复制到剪贴板！');
                } catch (err) {
                    console.error('复制失败:', err);
                }
            }

            retry() {
                this.generateContent();
            }
        }

        // 初始化应用
        const writingAssistant = new WritingAssistant();
    </script>
</body>
</html>